<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="请选择门店">
        <el-select v-model="storeId" placeholder="请选择门店" prop="storeId" @change="getList" >
          <el-option value="">全部门店</el-option>
          <el-option
            v-for="item in device_infoList"
            :key="item.id"
            :label="item.company"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="queryParams.categoryId" placeholder="一级分类" @change="getCategoryList" clearable style="width:100px">
          <el-option value="">一级分类</el-option>
          <el-option
            v-for="item in firstCategoryList"
            :key="item.cid"
            :label="item.categoryName"
            :value="item.cid"
          >
          </el-option>
        </el-select>
        <el-select v-model="queryParams.subclassId" placeholder="二级分类" clearable style="width:100px">
          <el-option value="">二级分类</el-option>
          <el-option
            v-for="item in subCategoryList"
            :key="item.cid"
            :label="item.categoryName"
            :value="item.cid"
          >
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="订单时间范围" label-width="200px">
        <el-date-picker
          v-model="daterangeOrderTime"
          style="width: 280px"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-value="new Date()"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energize:categorySaleReport:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categorySaleReportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="自动递增id" align="center" prop="autoId" />-->
<!--      <el-table-column label="分类一级ID" align="center" prop="categoryId" />-->
      <el-table-column label="一级分类名称" align="center" prop="categoryIdName" />
<!--      <el-table-column label="二级分类ID" align="center" prop="subclassId" />-->
      <el-table-column label="二级分类名称" align="center" prop="subclassIdName" />
      <el-table-column label="销售额" align="center" prop="sales" />
      <el-table-column label="销售数量" align="center" prop="salesQuantity" />
      <el-table-column label="采购价格" align="center" prop="purchasePrice" />
      <el-table-column label="利润" align="center" prop="profit" />
      <el-table-column label="利润率" align="center" prop="profitRatio" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listCategorySaleReport} from "@/api/energize/categorySaleReport";
import { listDevice_info } from "@/api/energize/device_info";
import { listCategory} from "@/api/energize/category";

export default {
  name: "CategorySaleReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备信息表格数据
      device_infoList: [],
      //烟草一级分类列表
      firstCategoryList:[],
      //烟草二级分类列表
      subCategoryList:[],
      // 烟草分类占比报告表格数据
      categorySaleReportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //门店id
      storeId:'',
      // 采购价格时间范围
      daterangeOrderTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: null,
        subclassId:null,
        createTime: null,
      },
      //获取分类列表查询参数
      categoryQueryParams:{
        pageNum: 1,
        pageSize: 5000,
        fatherId: 0,
        isDel: null,
        isDisplay: 1,//0不显示 1显示
      },
      pickerMinDate: '', //获取开始选择时间
      pickerMaxDate: '', //获取结束选择时间
      pickerOptions: {
        //时间范围选择控制
        onPick: ({ maxDate, minDate }) => {
          if (minDate) {
            this.pickerMinDate = minDate.getTime()
          }
          if (maxDate) {
            this.pickerMinDate = ''
            this.pickerMaxDate = maxDate.getTime()
          }
        },
        disabledDate: time => {
          const day32 = 32 * 24 * 3600 * 1000
          if (this.pickerMinDate !== '') {
            let maxTime = this.pickerMinDate + day32
            if (maxTime > new Date()) {
              maxTime = new Date()
            }
            return time.getTime() > maxTime || time.getTime() > Date.now() - 86400000
          }
          if (this.pickerMaxDate !== '' && !this.pickerMinDate) {
            let minTime = this.pickerMaxDate - day32
            return time.getTime() < minTime || time.getTime() > Date.now() - 86400000
          }
          return time.getTime() > Date.now() - 86400000
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // 设置时间为当月1号到当前日期的时间
    this.defaultDate();
    this.getStoreList();
    this.getCategoryList();
    this.getList();
  },
  methods: {
    defaultDate() {
      let date = new Date();
      let year = date.getFullYear().toString();
      let month =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1).toString()
          : (date.getMonth() + 1).toString();
      let da =
        date.getDate() < 10
          ? "0" + date.getDate().toString()
          : date.getDate().toString();
      let end = year + "-" + month + "-" + da;
      let beg = year + "-" + month + "-01";
      //将值设置给插件绑定的数据
      this.daterangeOrderTime.push(end);
      this.daterangeOrderTime.unshift(beg);
    },
    /**
     * 查询设备列表
     * */
    getStoreList(){
      listDevice_info().then(response => {
        this.device_infoList = response.rows;
      });
    }
    ,
    /**
     * 查询分类列表接口
     * */
    //queryParams.categoryId
    getCategoryList(){
      if(this.queryParams.categoryId!=null && this.queryParams.categoryId!=''){
        this.categoryQueryParams.fatherId=this.queryParams.categoryId;
        listCategory(this.categoryQueryParams).then(response => {
          this.subCategoryList = response.rows;
        });
      }else{
        this.categoryQueryParams.fatherId=0;
        listCategory(this.categoryQueryParams).then(response => {
          this.firstCategoryList = response.rows;
        });
      }

    },
     /** 查询烟草分类占比报告列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrderTime && '' != this.daterangeOrderTime) {
        this.queryParams.params["beginOrderTime"] = this.daterangeOrderTime[0];
        this.queryParams.params["endOrderTime"] = this.daterangeOrderTime[1];
      }else{
        this.defaultDate();
      }
      if (null != this.storeId && '' != this.storeId) {
        this.queryParams.params["storeId"] = this.storeId;
      }
      listCategorySaleReport(this.queryParams).then(response => {
        this.categorySaleReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        autoId: null,
        categoryId: null,
        subclassId: null,
        categoryIdName: null,
        subclassIdName: null,
        sales: null,
        salesQuantity: null,
        profit: null,
        profitRatio: null,
        purchasePrice: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.autoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /*/!** 新增按钮操作 *!/
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加烟草分类占比报告";
    },
    /!** 修改按钮操作 *!/
    handleUpdate(row) {
      this.reset();
      const autoId = row.autoId || this.ids
      getCategorySaleReport(autoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改烟草分类占比报告";
      });
    },
    /!** 提交按钮 *!/
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.autoId != null) {
            updateCategorySaleReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCategorySaleReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /!** 删除按钮操作 *!/
    handleDelete(row) {
      const autoIds = row.autoId || this.ids;
      this.$modal.confirm('是否确认删除烟草分类占比报告编号为"' + autoIds + '"的数据项？').then(function() {
        return delCategorySaleReport(autoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },*/
    /** 导出按钮操作 */
    handleExport() {
      this.download('energize/categorySaleReport/export', {
        ...this.queryParams
      }, `categorySaleReport_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
